<?php
	
		
		class friendAction extends userAction{
		
			private $feedSetting = false; 
			protected function _initialize() {
				parent::_initialize();
				$this->feedSetting = getAddFeedSetting($this->userInfo['id'],"friendaction"); 
			}
			
			/**
			  * 好友列表
			  *
			  **/
			public function index(){
				import("ORG.Util.Page"); //导入分页类
				isset($_GET["u"]) ? $u = safeRequest($_GET["u"]) : $u= $this->userInfo['id'];    //条件参数
				$this->rank =  getRank($this->userInfo['id'],$u,"friend");
				if(!$this->rank){
					redirect(__APP__."/rank/norank/u/".$u);
					return false;
				}
				isset($_GET["p"]) ? $p = $_GET["p"] : $p = 1;       //页数
				$dao = D("friendview");
				if(isset($_GET["ajax"])){
					$friendList	= $dao->findAll("friends.userid=".$u." and friends.friendid=user.id");
					$this->assign("friendlist",$friendList);
					$this->display("index4ajax");
				}else{
					$friendCount = D("cache")->find("userid=".$u,"friendCount")->friendCount; 
					$listRows = C("publicListRows"); 
					$p = new Page($friendCount,$listRows); 
					$p->setConfig('header' ,L('page_title']); 
					$friendList	= $dao->findAll("friends.userid=".$u." and friends.friendid=user.id","*",'lasttime desc',$p->firstRow.','.$p->listRows);
					$page  = $p->show();
					$this->assign("u",$u);
					$this->assign("friendlist",$friendList);
					$this->assign("page",$page);
					$this->display();
				}
			}
			
			/**
			  * 发送好友请求
			  *
			  **/
			public function sendrequest(){
				$u = safeRequest($_GET["u"]);
				if($u == $this->userInfo['id']){
					isset($_GET["ajax"]) ? print "error" : $this->error(L("no_add_self"));
				}else{
					$dao = D("friends_requests");
					if($dao->find("requestid=".$this->userInfo['id']." and userid=".$u)){
						isset($_GET["ajax"]) ? print "error1" : $this->error(L("has_request"));
					}else{
						if(D("friends")->find("userid=".$this->userInfo['id']." and friendid=".$u)){
							isset($_GET["ajax"]) ? print "error2" : $this->error(L("has_friend"));
						}else{
							$dao->requestid = $this->userInfo['id'];
							$dao->userid = $u;
							$id = $dao->add();
							if($id>0){
								isset($_GET["ajax"]) ? print "requestsuccess" : $this->success(L("request_success"));
							}else{
								isset($_GET["ajax"]) ? print "error3" : $this->error(L("request_error"));
							}
						}
					}
				}
			}
			
			/**
			  * 确认好友
			  *
			  **/
			public function checkrequest(){
				$u = safeRequest($_GET["u"]);
				$dao = D("friends_requests");
				$record = $dao->find("requestid=".$u." and userid=".$this->userInfo['id'],"id");
				if($record){
					$friend = D("friends");
					$data[0]["userid"]   =  $u;
					$data[0]["friendid"] =  $this->userInfo['id'];
					$data[0]["lasttime"] =  time();
					$data[1]["userid"]   =  $this->userInfo['id'];
					$data[1]["friendid"] =  $u;
					$data[1]["lasttime"] =  time();
					$id= $friend->addAll($data);//添加好友					
					if($id>0){
						
						//更新好友的好友列表cache
						$friendList = $friend->findAll("userid=".$u); //
						F("friendList",$friendList,-1,getCachePath(APP_PATH."/UserCache",$u));
						
						//更新自己的好友列表cache
						$friendList = $friend->findAll("userid=".$this->userInfo['id']);
						F("friendList",$friendList,-1,getCachePath(APP_PATH."/UserCache",$this->userInfo['id']));

						D("cache")->setField("friendCount","(friendCount+1)","(userid=".$u.") or (userid=".$this->userInfo['id'].")");
						addNotify($u,$this->userInfo['id'],"checkfriend",$id,0);   //通知
						$dao->delete("id=".$record['id']);//删除请求
						if($this->feedSetting){
							addFeed($this->userInfo['id'],"friend",$u,0,$u); //动态
						}
						isset($_GET["ajax"]) ? print "successfriend" : $this->success(L("check_success"));
					}else{
						isset($_GET["ajax"]) ? print "error" : $this->error(L("check_error"));
					}
				}else{
					isset($_GET["ajax"]) ? print "error" : $this->error(L("check_error"));
				}
			}

			/**
			  * 删除好友
			  *
			  **/
			public function del(){
				$u = safeRequest($_GET["u"]);
				if(D("friends")->delete("(userid=".$this->userInfo['id']." and friendid=".$u.") or(userid=".$u." and friendid=".$this->userInfo['id'].")")){
					D("cache")->setField("friendCount","(friendCount-1)","(userid=".$u.") or (userid=".$this->userInfo['id'].")");
					
					//更新好友列表cache
					$friendList = $friend->findAll("userid=".$this->userInfo['id']);
					F("friendList",$friendList,-1,getCachePath(APP_PATH."/UserCache",$this->userInfo['id']));
					
					isset($_GET["ajax"]) ? print "success" : $this->success(L("delete_success"));
				}else{
					isset($_GET["ajax"]) ? print "error" : $this->error(L("delete_success"));
				}
			}
			
			//搜索好友
			public function search(){
				$username = safeRequest($_POST["searchname"],1);
				if($username=="" || $username=="输入人名"){
					$this->error("人名不能为空");
				}else{
					$list = D("user")->findAll("username like '%".$username."%'","id,username,network_city");
					$this->assign("list",$list);
					$this->display();
				}
			}

			//好友网络分布
			public function friendnetwork(){
			}
			
			//邀请好友
			public function invitefriend(){
				$this->display();
			}

			public function postinvite(){
				if(isset($_POST["email"])){
					import("@.Util.PHPMailer.PHPMailer");
					import("@.Util.PHPMailer.SMTP");
					import("@.Util.PHPMailer.PHPMailerUtil");
					$mailArr = explode(",",$_POST["email"]);
					$fromName = $this->userInfo['username'];
					$title = $this->userInfo['username'].'邀请你加入';
					$style = '<style>.friendbody{width:600px;height:230px;padding:5px;border:1px solid #ccc;}.friendface{float:left;width:70px;}.friendcontent{float:right;width: 525px;line-height:2em;}.friendcontent p{margin:0pt;padding:0pt;}</style>';
					$content = $style.'<div class="friendbody"><div class="friendface"><img src="http://demo.guangla.com'.WEB_PUBLIC_URL.'/Uploads/'.$this->userInfo['id'].'/face/small_face.jpg"/></div>';
					$content .= '<div class="friendcontent"><p><b>'.$this->userInfo->username.'邀请你加入guangla，</b></p>';
					$dao = D("user_invite");
					for($i=0;$i<count($mailArr);$i++){
						$md5 = md5(uniqid(rand(), true));
						$url = '<p>逛啦是一个真人网络，提供个人空间，迷你博客，相册，群组等服务。<br/>他对你说：'.$_POST["message"].'<br/>请点击下面的链接注册并成为'.$this->userInfo->username.'的好友<br/><a href="http://demo.guangla.com'.__APP__.'/login/invite/u/'.$this->userInfo['id'].'/g/'.$md5.'">http://demo.guangla.com'.__APP__.'/login/invite/u/'.$this->userInfo['id'].'/g/'.$md5.'</a><br/>逛啦 (guangla.com)<br/><p></div></div>';
						$dao->userid = $this->userInfo['id'];
						$dao->md5 = $md5;
						$dao->tomail = $mailArr[$i];
						$dao->regtime = time();
						if($dao->add()){
							PHPMailerUtil::send($fromName, $mailArr[$i], $title, $content.$url,true);
						}
					}
					$this->success(L("invite_success"));
				}else{
					$this->error(L("invite_error"));
				}
			}

			private function getMSN($username,$password) {
				set_time_limit(120);
				import('@.Util.MSN');
				$msn	=	new msn;
				return $msn->qGrab($username, $password);
			}
			private function get163($username,$password) {
				set_time_limit(120);
				import('@.util.Grabber_163');
				$get163	=	new Grabber_163;
				return $get163->get($username, $password);
			}
		}
?>